﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.BlackApart.Business;
using System.Configuration;

namespace blackhouse.BlackApart.Web.Mails {
    public partial class HtmlMailTemplate : System.Web.UI.UserControl {

        #region Properties

        public IWebReservation Reservation { get; set; }
        public Factory factory = Factory.Instance();
        #endregion

        #region Methods

        public string MakeUrl(string relativeUrl) {
            relativeUrl = UrlUtils.LocalizeUrl(factory.GetPreFolderEvaluation(), relativeUrl);
            relativeUrl = FullyQualifiedApplicationPath + relativeUrl;
          
            return relativeUrl;
        }

        private string FullyQualifiedApplicationPath {
            get {
                var appPath = ConfigurationManager.AppSettings["Domain"];
                var context = HttpContext.Current;
                if (context != null) {
                    appPath = string.Format("{0}://{1}{2}{3}",
                                            context.Request.Url.Scheme,
                                            context.Request.Url.Host,
                                            context.Request.Url.Port == 80
                                                ? string.Empty
                                                : ":" + context.Request.Url.Port,
                                            context.Request.ApplicationPath);
                }

                if (appPath.EndsWith("/"))
                    appPath = appPath.Substring(0, appPath.Length - 1);

                if (!appPath.StartsWith("http"))
                    appPath = "http://" + appPath;
                return appPath;
            }
        }
        #endregion

    }
}